<?php

##################################################
#
# Copyright (c) 2009 Immanuel192 - ABSoft
# 7/23/2009: Make smart redirect sync better when works with SEF
// Bail in case someone has visited us directly, or the Pathos framework is
// otherwise not initialized.
if (!defined('EXPONENT')) exit('');

// Initialize the Exponent Framework
require_once('exponent.php');

// Initialize the Sessions Subsystem
if (!defined('SYS_SESSIONS')) require_once(BASE.'subsystems/sessions.php');

$lang_act=0;

if (exponent_lang_exist($_GET['lid']) == 1)
{
exponent_sessions_set('language', $_GET['lid']);
$lang=exponent_sessions_get('language');
$lang_act=1;
$lang_number=exponent_lang_get_number($_GET['lid']);
}
$url=exponent_flow_get();
$last_section = exponent_sessions_get("last_section");

echo $url ."<br>";
echo "section=".$last_section;

if ($lang_act == 1)
{
  if (SEF_URLS == 1)
  { // here we will search for section id
    $find_section=strrpos($url,"/");
    $find_section = substr($url, $find_section+1);
    $temp= $db->selectObject('section','sef_name="'. $find_section.'"');
    if ($temp)
      $findex=1; // fake that $findex > 0
  }
  else
  { // not in SEF_URL mode; search normal
    $findex=strpos($url, "section=".$last_section);
  }
  if ($findex > 0 )
  {
	 $last_section=exponent_lang_get_number($lang) . substr($last_section,1);
	 $temp= $db->selectObject('section','id='. $last_section );
  	if ($temp) // check if page exist
	  	$url= URL_FULL.'index.php?section='.$last_section;
  }
}

header("Location: {$url}");	
exit('Changing language...');

?>
